﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using Users;
using Lists;
using Sim;

namespace Sim
{
    [Serializable()]
    public class Tournament
    {
        public string title { get; set; }
        public bool isStarted { get; set; }
        public string date { get; set; }
        public int jumpHeight { get; set; }
        public string sex { get; set; }
        public int totalJudges { get; set; }
        public List<Judge> judgeList { get; set; }
        public int totalDivers { get; set; }
        public List<Diver> diverList { get; set; }

        public Tournament()
        {
            this.isStarted = false;
            this.title = "";
            this.date = "";
            this.jumpHeight = 0;
            this.sex = "";
            this.totalJudges = 0;
            this.totalDivers = 0;
            judgeList = new List<Judge>();
            diverList = new List<Diver>();
        }

        public Tournament(string tourneyName, string date, int jumpHeight, string sex, int totalJudges, int totalDivers)
        {
            this.isStarted = false;
            this.title = tourneyName;
            this.date = date;
            this.jumpHeight = jumpHeight;
            this.sex = sex;
            this.totalJudges = totalJudges;
            this.totalDivers = totalDivers;
            judgeList = new List<Judge>();
            diverList = new List<Diver>();
        }

        public int AddDiver(Diver d)
        {
            for (int i = 0; i < diverList.Count(); i++)
            {
                if (d.username == diverList[i].username)
                {
                    return 0;
                }
            }
            for (int i = 0; i < UserList.userList.Count(); i++)
            {
                if (d.username == UserList.userList[i].username && d.level == 0 && UserList.userList[i].level == 0)
                {
                    d.id = UserList.userList[i].id;
                    diverList.Add(d);
                    break;
                }
            }
            return 1;
        }

        public int AddDiver(string username)
        {
            for (int i = 0; i < diverList.Count(); i++)
            {

                if (username == diverList[i].username)
                {
                    return 0;
                }
            }
            for (int i = 0; i < UserList.userList.Count(); i++)
            {
                if (username == UserList.userList[i].username && UserList.userList[i].level == 0)
                {
                    Diver d = new Diver();
                    d.id = UserList.userList[i].id;
                    d.firstName = UserList.userList[i].firstName;
                    d.lastName = UserList.userList[i].lastName;
                    d.age = UserList.userList[i].age;
                    d.nation = UserList.userList[i].nation;
                    d.sex = UserList.userList[i].sex;
                    d.username = UserList.userList[i].username;
                    this.diverList.Add(d);
                    break;
                }
            }
            return 1;
        }

        public void RemoveDiver(int index)
        {
            diverList.RemoveAt(index);
        }

        public int AddJudge(Judge j)
        {
            for (int i = 0; i < judgeList.Count(); i++)
            {
                if (j.username == judgeList[i].username)
                {
                    return 0;
                }
            }
            for (int i = 0; i < UserList.userList.Count(); i++)
            {
                if (j.username == UserList.userList[i].username && j.level == 1 && UserList.userList[i].level == 1)
                {
                    j.id = UserList.userList[i].id;
                    judgeList.Add(j);
                    break;
                }
            }
            return 1;
        }

        public int AddJudge(string username)
        {
            for (int i = 0; i < judgeList.Count(); i++)
            {
                if (username == judgeList[i].username)
                {
                    return 0;
                }
            }
            for (int i = 0; i < UserList.userList.Count(); i++)
            {
                if (username == UserList.userList[i].username && UserList.userList[i].level == 1)
                {
                    Judge j = new Judge();
                    j.id = UserList.userList[i].id;
                    j.firstName = UserList.userList[i].firstName;
                    j.lastName = UserList.userList[i].lastName;
                    j.age = UserList.userList[i].age;
                    j.nation = UserList.userList[i].nation;
                    j.sex = UserList.userList[i].sex;
                    j.username = UserList.userList[i].username;
                    judgeList.Add(j);
                    break;
                }
            }
            return 1;
        }

        public void RemoveJudge(int index)
        {
            judgeList.RemoveAt(index);
        }

        public void SendDive(string diveCode)
        {
            
        }
    }
}